1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#![allow(non_camel_case_types, non_snake_case)]
use crate::co;
use crate::decl::*;
use crate::guard::*;
use crate::kernel::ffi;
use crate::prelude::*;
impl_handle! { HFINDFILE;
/// Handle to a
/// [file search](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstfilew).
/// Originally just a `HANDLE`.
}
impl kernel_Hfindfile for HFINDFILE {}
/// This trait is enabled with the `kernel` feature, and provides methods for
/// [`HFINDFILE`](crate::HFINDFILE).
///
/// Prefer importing this trait through the prelude:
///
/// ```no_run
/// use winsafe::prelude::*;
/// ```
pub trait kernel_Hfindfile: Handle {
/// [`FindFirstFile`](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstfilew)
/// function.
///
/// This method is rather tricky, consider using
/// [`path::dir_list`](crate::path::dir_list).
#[must_use]
fn FindFirstFile(
file_name: &str,
wfd: &mut WIN32_FIND_DATA,
) -> SysResult<(FindCloseGuard, bool)>
{
unsafe {
match ffi::FindFirstFileW(
WString::from_str(file_name).as_ptr(),
wfd as *mut _ as _,
).as_mut() {
Some(ptr) => {
let h = HFINDFILE::from_ptr(ptr);
// When using a filter, if no files are found, the function
// is successful but it returns an invalid handle.
let has_something = h != HFINDFILE::INVALID;
Ok((
FindCloseGuard::new(h),
has_something,
))
},
None => match GetLastError() {
co::ERROR::FILE_NOT_FOUND => Ok((
FindCloseGuard::new(HFINDFILE::NULL), // not an error, first file not found
false,
)),
err => Err(err),
},
}
}
}
/// [`FindNextFile`](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findnextfilew)
/// function.
///
/// This method is rather tricky, consider using
/// [`path::dir_list`](crate::path::dir_list).
#[must_use]
fn FindNextFile(&self, wfd: &mut WIN32_FIND_DATA) -> SysResult<bool> {
match unsafe { ffi::FindNextFileW(self.ptr(), wfd as *mut _ as _) } {
0 => match GetLastError() {
co::ERROR::NO_MORE_FILES => Ok(false), // not an error, no further files found
err => Err(err),
},
_ => Ok(true),
}
}
}